class QaDetailsController < ApplicationController
  # GET /qa_details
  # GET /qa_details.xml
  #
  # url: 10.10.10.154:3000/grades.json
  # params structure:
  # question_id = 1
  def index
    @qa_details = QaDetail.question(params[:question_id])

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @qa_details }
      format.json { render :json => @qa_details}
    end
  end


  def get_conversation
    teacher_id = params[:user_id]
    question_id = params[:question_id]

    qas = QaDetail.find(:all,      
      :conditions => ['question_id = ? AND (user_id = ? OR rely_to = ?)', question_id, teacher_id, teacher_id],
      :order => 'created_at'
    ).collect{|qa|
      attrs = qa.attributes
      attrs
    }

    QaDetail.update_all("is_read = true",["id IN (?)",qas.collect{|qa| qa['id']}])

    render :json => {
      :question => Question.find(question_id).attributes,
      :qa_details => qas
    }
  end

  # POST /qa_details
  # POST /qa_details.xml
  def create
    @qa_detail = QaDetail.new(params[:qa_detail])
    #@qa_detail.user_id = current_user.id

    respond_to do |format|
      if @qa_detail.save
        student = @qa_detail.question.user
        student.point -= 333
        student.save 
        flash[:notice] = 'QaDetail was successfully created.'
        format.html { redirect_to(@qa_detail) }
        format.xml  { render :xml => @qa_detail, :status => :created, :location => @qa_detail }
        format.json { render :json => {:result => RESPONSE_MSG[:success]}}
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @qa_detail.errors, :status => :unprocessable_entity }
        format.json { render :json => {:result => RESPONSE_MSG[:error]}}
      end
    end
  end

  def upload
    puts "params: #{params.inspect}"
    render :json => params
  end
end
